package com.robot.log.service;

import com.robot.log.dao.SearchLogDao;
import com.robot.log.pojo.SearchLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * searchLog服务层
 *
 * @author wuyunbin
 */
@Service
public class SearchLogService {
    @Autowired
    private SearchLogDao searchLogDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<SearchLog> findAll() {
        return searchLogDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<SearchLog> findSearch(Map whereMap, int page, int size) {
        Specification<SearchLog> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return searchLogDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<SearchLog> findSearch(Map whereMap) {
        Specification<SearchLog> specification = createSpecification(whereMap);
        return searchLogDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public SearchLog findById(String id) {
        return searchLogDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param searchLog
     */
    public void add(SearchLog searchLog) {
        //雪花分布式ID生成器
        searchLog.setId(idWorker.nextId() + "");
        searchLog.setCreatedAt(LocalDateTime.now());
        searchLogDao.save(searchLog);
    }

    /**
     * 修改
     *
     * @param searchLog
     */
    public void update(SearchLog searchLog) {
        searchLogDao.save(searchLog);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        searchLogDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<SearchLog> createSpecification(Map searchMap) {
        return (Specification<SearchLog>) (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<Predicate>();
            // id
            String id = "id";
            if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
            }
            // merchant_id
            String companyId = "companyId";
            if (searchMap.get(companyId) != null && !"".equals(searchMap.get(companyId))) {
                predicateList.add(cb.like(root.get(companyId).as(String.class), "%" + searchMap.get(companyId) + "%"));
            }
            // keywords
            String keywords = "keywords";
            if (searchMap.get(keywords) != null && !"".equals(searchMap.get(keywords))) {
                predicateList.add(cb.like(root.get(keywords).as(String.class), "%" + searchMap.get(keywords) + "%"));
            }
            // 省份
            String province = "province";
            if (searchMap.get(province) != null && !"".equals(searchMap.get(province))) {
                predicateList.add(cb.like(root.get(province).as(String.class), "%" + searchMap.get(province) + "%"));
            }
            // 省份id
            String provinceId = "provinceId";
            if (searchMap.get(provinceId) != null && !"".equals(searchMap.get(provinceId))) {
                predicateList.add(cb.like(root.get(provinceId).as(String.class), "%" + searchMap.get(provinceId) + "%"));
            }
            // 城市
            String city = "city";
            if (searchMap.get(city) != null && !"".equals(searchMap.get(city))) {
                predicateList.add(cb.like(root.get(city).as(String.class), "%" + searchMap.get(city) + "%"));
            }
            // 城市id
            String cityId = "cityId";
            if (searchMap.get(cityId) != null && !"".equals(searchMap.get(cityId))) {
                predicateList.add(cb.like(root.get(cityId).as(String.class), "%" + searchMap.get(cityId) + "%"));
            }
            // 区县
            String area = "area";
            if (searchMap.get(area) != null && !"".equals(searchMap.get(area))) {
                predicateList.add(cb.like(root.get(area).as(String.class), "%" + searchMap.get(area) + "%"));
            }
            // 区县id
            String areaId = "areaId";
            if (searchMap.get(areaId) != null && !"".equals(searchMap.get(areaId))) {
                predicateList.add(cb.like(root.get(areaId).as(String.class), "%" + searchMap.get(areaId) + "%"));
            }
            //剔除软删除数据
            String deletedAt = "deletedAt";
            predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

            return cb.and(predicateList.toArray(new Predicate[0]));
        };
    }

    /**
     * 根据memberId和keywords查找搜索记录,同一用户搜索相同关键词只记录一次
     *
     * @param memberId
     * @param keywords
     * @return
     */
    public SearchLog findByMemberIdAndKeywords(String memberId, String keywords) {
        return searchLogDao.findByMemberIdAndKeywords(memberId, keywords);
    }

    /**
     * 查找最热门的10条数据
     *
     * @return
     */
    public List<String> findTop10SearchLogsDesc() {
        return searchLogDao.findTop10SearchLogsDesc();
    }
}
